package com.xiemulei.gulimall.seckill.schedule;

import com.xiemulei.gulimall.seckill.service.SeckillService;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import jakarta.annotation.Resource;
import java.util.concurrent.TimeUnit;

@Slf4j
@Service
@EnableScheduling
public class SeckillSkuSchedule {
    @Resource
    SeckillService seckillService;

    @Resource
    RedissonClient redissonClient;

    private final String uploda_lock = "seckill:upload:lock";

    @Scheduled(cron = "0 0 0,12 * * ? ")
//    @Scheduled(cron = "0 0/1 * * * ? ")
    public void uploadSeckillSkuLatest3Days() {
        log.info("上架秒杀的商品...");

        RLock lock = redissonClient.getLock(uploda_lock);
        lock.lock(10, TimeUnit.SECONDS);
        try {
            seckillService.uploadSeckillSkuLatest3Days();
        } finally {
            lock.unlock();
        }
    }
}
